<?php
namespace Modules\Admin\Service;
use Modules\Admin\Entities\Module;

class ModuleService{
	public function updateCache(){
		\DB::table ('modules')->truncate ();
		$modules = \HDModule::getModulesLists();
		foreach ($modules as $v){
			Module::create([
				'name' => $v['name'],
				'title' => $v['title'],
				'is_default' => 0,
				'home_access' => $this->is_home_access($v),
			]);
		}
	}
	public function is_home_access($module){
		$class = '\Modules\\' .$module['name'].'\\Http\\Controllers\\HomeController';
		return class_exists ($class) && method_exists ($class,'index');
	}

	public function getDefault(){
		$module = Module::where([
			['home_access',1],
			['is_default',1]
		])->first();
		$module = $module ? $module : Module::where('name','Shop')->first();
		return $module;
	}
}
?>